<extend name="Manage/themes/default/base" />
<block name="main">
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15" style="position: fixed;width: 99%;z-index: 99999;top: 0.07rem">
            <div class="layui-col-md12">
                <div class="search-toolbar layui-form">
                    <div class="search-toolbar-left">
                        <form class="layui-form layui-col-space5">
                            <div class="layui-inline search-toolbar-item" style="width: 150px">
                                <span class="layui-inline">
                                    <select id="module_id" class="layui-input">
                                        <option value="-1">所属功能</option>
                                        <option value="0">不限</option>
                                        <foreach name="moduleList"  key="k" item="value" >
                                            <option value="{{$value.id}}">{{$value.name}}</option>
                                        </foreach>
                                    </select>
                                </span>
                            </div>
                            <div class="layui-inline search-toolbar-item" style="width: 150px">
                                <span class="layui-inline">
                                    <select id="clients_id" class="layui-input">
                                        <option value="-1">所属客户端</option>
                                        <option value="0">不限</option>
                                        <foreach name="clientList"  key="k" item="value" >
                                            <option value="{{$value.id}}">{{$value.name}}</option>
                                        </foreach>
                                    </select>
                                </span>
                            </div>
                            <div class="layui-inline search-toolbar-item" style="width: 100px">
                                <span class="layui-inline">
                                    <select id="is_menu" class="layui-input">
                                        <option value="-1">是否菜单</option>
                                        <option value="0">否</option>
                                        <option value="1">是</option>
                                    </select>
                                </span>
                            </div>
                            <div class="layui-inline search-toolbar-item" style="width: 100px">
                                <span class="layui-inline">
                                    <select id="is_show" class="layui-input">
                                        <option value="-1">是否显示</option>
                                        <option value="0">否</option>
                                        <option value="1">是</option>
                                    </select>
                                </span>
                            </div>
                            <div class="layui-inline search-toolbar-item">
                                <button type="button" class="layui-btn search-btn" onclick="List.searchList()">查询</button>
                            </div>
                        </form>
                    </div>
                    <div class="search-toolbar-right">
                        <a href="javascript:void(0)" onclick="List.openAddWindow(0)" class="layui-btn layui-btn-green">添加顶级权限</a>
                    </div>
                </div>

            </div>
        </div>

        <div class="layui-card" style="margin-top: 0.5rem">
            <div class="layui-card-body ">
                <table class="layui-table" id="list" lay-filter="list"></table>
            </div>
        </div>

    </div>

    <script type="text/html" id="barRows">
        <a class="layui-btn layui-btn-xs" style="background-color: #5FB878;" lay-event="addSon"><i class="layui-icon">&#xe654;</i>添加子权限</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</a>
        <a class="layui-btn-danger layui-btn layui-btn-xs" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</a>
    </script>
    <script>
        var table,laypage,treetable,form;
        layui.use(['table','laypage','form'], function(){
            table = layui.table;
            laypage = layui.laypage;
            form = layui.form;

            form.on('switch(is_menu)', function (data) {
                var right_id = $(this).data('id');
                var value=data.elem.checked?1:0;
                List.switchIsMenu(right_id, value);
            });

            form.on('switch(is_show)', function (data) {
                var right_id = $(this).data('id');
                var value=data.elem.checked?1:0;
                List.switchIsShow(right_id, value);
            });

        });

        var treeData = {};
        layui.config({
            base: '{{$__STATIC__}}/themes/default/plugins/layui/lay/modules/'
        }).use(['treeTable'], function () {
            var $ = layui.jquery;
            treeTable = layui.treeTable;
            treeTable.on('tool(list)', function (obj) {
                List.rowBarEvent(obj);
            });
            // 渲染表格
            treeObj = treeTable.render({
                elem: '#list',
                toolbar: false,
                tree: {
                    iconIndex: 0,
                    idName: 'id',       // 自定义id字段的名称
                    pidName: 'parent_id',         // 自定义标识是否还有子节点的字段名称
                    isPidData: true              // 是否是pid形式数据
                },
                url: List.getDataUrl(),
                cols: [[
                    {field: 'right_name', title: '名称', width:300},
                    {field: 'right_code', title: '编号'},
                    {field: 'is_menu', title: '菜单',width:100, align:"center", templet: function(d){
                            var str ='';
                            var isChecked = (d.is_menu==1)?' checked':'';
                            str+='<input type="checkbox" lay-text="是|否" lay-skin="switch"'+isChecked+' lay-filter="is_menu" data-id="'+d.id+'">';
                            return str;
                        }},
                    {field: 'is_show', title: '显示',width:100, align:"center", templet: function(d){
                            var str ='';
                            var isChecked = (d.is_show==1)?' checked':'';
                            str+='<input type="checkbox" lay-text="是|否" lay-skin="switch"'+isChecked+' lay-filter="is_show" data-id="'+d.id+'">';
                            return str;
                        }},
                    {field: 'clients_name', align:"center",title: '客户端'},
                    {field: 'module_name', align:"center",title: '所属功能'},
                    {field: 'remark', title: '备注'},
                    {field: 'sort', title: '排序',width:100,align:"center"},
                    {title: '操作',toolbar:"#barRows",width:320,align:"center"},
                ]],
                parseData:function(data){
                    data['code'] = 0;
                    var list = data.data;
                    for(var i=0;i<list.length;i++){
                        list[i]['haveChild'] = list[i]['child_num']>0 ? true : false;
                        list[i]['open'] = true;
                    }
                    data.data = list;
                    return data;
                },
                style: 'margin-top:0;'
            });

        });


        var List = {
            canOperate:true,
            rowBarEvent:function(obj){
                var data = obj.data;
                switch(obj.event){
                    case 'addSon':
                        List.openAddWindow(data.id);
                        break;
                    case 'edit':
                        List.openDetailWindow(data.id);
                        break;
                    case 'delete':
                        List.deleteData(data.id);
                        break;
                }
            },
            searchParam:{ //搜索字段
                'module_id':-1,
            },

            getDataUrl:function(){
                var urlQuery = '';
                for(var i in List.searchParam){
                    urlQuery += i+'='+List.searchParam[i]+'&';
                }
                return VK.U('modules/rights?'+urlQuery);
            },

            /**
             * 切换是否菜单
             */
            switchIsMenu:function(id, value){
                if(!List.canOperate){
                    return false;
                }
                List.canOperate = false;
                VK.QAjax(VK.U('modules/rights/ismenu'),'put',{'id':id,'value':value},function(res){
                    List.canOperate = true;
                    if(res.status!=200){
                        VK.error(res.msg);
                        return false;
                    }
                },function(res){
                    List.canOperate = true;
                    VK.error('请求出错请重试');
                });
            },
            /**
             * 切换是否显示
             */
            switchIsShow:function(id, value){
                if(!List.canOperate){
                    return false;
                }
                List.canOperate = false;
                VK.QAjax(VK.U('modules/rights/isshow'),'put',{'id':id,'value':value},function(res){
                    List.canOperate = true;
                    if(res.status!=200){
                        VK.error(res.msg);
                        return false;
                    }
                },function(res){
                    List.canOperate = true;
                    VK.error('请求出错请重试');
                });
            },

            openAddWindow:function(parent_id){
                var url = VK.U('modules/rights/pages/0?parent_id='+parent_id);
                VK.openPoerWindow('权限设置', url, 0, 900,1080);
            },

            openDetailWindow:function(id){
                var url = VK.U('modules/rights/pages/'+id);
                VK.openPoerWindow('权限设置', url, 0, 800,1080);
            },

            deleteData:function(id){
                VK.confirm({content:"确定删除",yes:function(){
                        layer.closeAll();
                        List.canOperate = false;
                        var loading = VK.load();
                        VK.QAjax(VK.U('modules/rights/'+id),'delete',{},function(res){
                            VK.closeLoad(loading);
                            List.canOperate = true;
                            if(res.status!= 200){
                                VK.error(res.msg);
                                return false;
                            }
                            VK.success('删除成功');
                            treeObj.refresh();
                        },function(res){
                            List.canOperate = true;
                            VK.error('请求出错请重试');
                        });
                    }});
            },
            /**
             * 搜索
             */
            searchList:function(){
                List.searchParam.module_id = $('#module_id').val();
                List.searchParam.clients_id = $('#clients_id').val();
                List.searchParam.is_menu = $('#is_menu').val();
                List.searchParam.is_show = $('#is_show').val();
                var url = List.getDataUrl();
                treeObj.reload({url:url});
            },
        }

        function reloadList(){
            var url = List.getDataUrl();
            treeObj.reload({url:url});
        }
    </script>
</block>